<?php
/**
 * Created by PhpStorm.
 * User: wangchaozhen
 * Date: 2018/6/1
 * Time: 23:16
 */

namespace App\Service\Cart;

use App\Models\Cart\CartModel;
use App\Service\Goods\GoodsService;

class CartService
{
    /**
     * 添加进入购物车
     */
    public static function intoCart($goods_id,$uid,$number)
    {
        return CartModel::insert([
            'goods_id' => $goods_id,
            'buy_uid' => $uid,
            'number' => $number,
            'created_at' => date("Y-m-d H:i:s",time())
        ]);
    }

    /**
     * 购物车列表
     */
    public static function getList($uid)
    {
        $result = CartModel::join('lsj_goods','lsj_goods.id','=','lsj_shop_cart.goods_id')
            ->where('lsj_shop_cart.buy_uid',$uid)
            ->select('lsj_goods.thumb','lsj_goods.name','lsj_goods.orgin_price','lsj_goods.current_price'
            ,'lsj_goods.stock','lsj_goods.spec','lsj_goods.is_up','lsj_shop_cart.id','lsj_shop_cart.number')
            ->orderBy('lsj_shop_cart.created_at','desc')
            ->get();

        return $result;
    }

    /**
     * 删除购物车
     */
    public static function delCarts($ids, $uid)
    {
        return CartModel::whereIn('id',$ids)->where('buy_uid',$uid)->delete();
    }

    /**
     * 清空购物车
     */
    public static function emptyCarts($uid)
    {
        return CartModel::where('buy_uid',$uid)->delete();
    }

    /**
     * 修改商品数量
     */
    public static function changeCartNums($cart_id,$uid,$number)
    {
        return CartModel::where([
            'id' => $cart_id,
            'buy_uid' => $uid
        ])->update(['number'=>$number]);
    }

    /**
     * 获得购物车中商品信息
     */
    public static function getCartGoodsInfo($cart_id,$uid)
    {
        $cart_info = CartModel::where([
            'id' => $cart_id,
            'buy_uid' => $uid
        ])->first();

        if(!$cart_info){
            return false;
        }

        $goods_info = GoodsService::getGoodsIdInfo($cart_info['goods_id']);

        return $goods_info;
    }

    /**
     * 购物车信息
     */
    public static function getCartInfo($goods_id, $uid)
    {
        $cart_info = CartModel::where([
            'goods_id' => $goods_id,
            'buy_uid' => $uid
        ])->first();

        return $cart_info;
    }

    /**
     * 根据cart_ids获得购物车商品
     */
    public static function getGoodsInCart($cart_ids,$uid)
    {
        $result = CartModel::join('lsj_goods','lsj_goods.id','=','lsj_shop_cart.goods_id')
            ->whereIn('lsj_shop_cart.id',$cart_ids)
            ->where('buy_uid',$uid)
            ->where('is_up','1')
            ->where('stock','>',0)
            ->where('stock','>','number')
            ->select(
                'lsj_goods.spec','lsj_goods.thumb','lsj_goods.name','lsj_goods.orgin_price','lsj_goods.description',
                'lsj_goods.description','lsj_goods.current_price','lsj_shop_cart.goods_id',
                'lsj_shop_cart.number','lsj_shop_cart.buy_uid','lsj_goods.stock',\DB::raw('lsj_goods.current_price*lsj_shop_cart.number as price')
            )
            ->get()->toArray();

        return $result;
    }

}